package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.text.TextUtils;

import com.quasar.hpatient.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.context.BaseConstant;
import lib.quasar.util.CalendarUtil;
import lib.quasar.widget.menu.NavigationView;
import lib.quasar.widget.wheel.WheelView;


/**
 * description: 年-月-日
 * created by kalu on 2018/3/29 9:03
 */
public class DateDialog extends BaseDialog {

    private final LinkedList<String> yearList = new LinkedList<>();
    private final List<String> monthList = Arrays.asList("01", "02", "03", "04", "05",
            "06", "07", "08", "09", "10", "11", "12");
    private final ArrayList<String> dayList = new ArrayList<>();
    /************************************************/

    private OnDateChangeListener listener;

    public DateDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_date;
    }

    @Override
    protected void initData() {

        final WheelView year = findViewById(R.id.dialog_date_year);
        final WheelView month = findViewById(R.id.dialog_date_month);
        final WheelView day = findViewById(R.id.dialog_date_day);

        final NavigationView menu = findViewById(R.id.dialog_date_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) cancel();
            else if (isMenu) {

                if (null != listener) {
                    listener.onDateChange(year.getCurText(), month.getCurText(), day.getCurText());
                }
                cancel();
            }
        });

        // 1.年
        final String year1 = CalendarUtil.getYears();
        final int begin = Integer.parseInt(year1) - 20;
        final int end1 = Integer.parseInt(year1) + 20;
        for (int i = begin; i <= end1; i++) {
            yearList.addLast(String.valueOf(i));
        }
        year.setLoop(true);
        year.setList(yearList);
        year.setDefault(year1);
        year.setOnWheelChangeListener((index, str) -> calcuDate(day, Integer.parseInt(str), Integer.parseInt(month.getCurText())));
        // 2.月
        month.setLoop(true);
        month.setList(monthList);
        month.setDefault(CalendarUtil.getMonth());
        month.setOnWheelChangeListener((index, str) -> calcuDate(day, Integer.parseInt(year.getCurText()), Integer.parseInt(str)));
        // 3.日
        day.setLoop(true);
        calcuDate(day, Integer.parseInt(year1), CalendarUtil.getMonth());
    }

    private final void calcuDate(WheelView wheel, int year, int month) {

        final Calendar calendar = CalendarUtil.getCalendar();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);//Java月份才0开始算
        int dateOfMonth = calendar.getActualMaximum(Calendar.DATE);

        dayList.clear();
        for (int i = 1; i <= dateOfMonth; i++) {

            if (i < 10) {
                dayList.add(BaseConstant.ZERO + i);
            } else {
                dayList.add(String.valueOf(i));
            }
        }

        wheel.setList(dayList);
        wheel.setDefault(CalendarUtil.getDay());
    }

    public void setTitles(String str) {

        if (TextUtils.isEmpty(str))
            return;

        final NavigationView menu = findViewById(R.id.dialog_date_menu);
        menu.setTitle(str);
    }

    public void setOnDateChangeListener(OnDateChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDateChangeListener {
        void onDateChange(String year, String month, String day);
    }
}
